import {
  gbs
} from '../../config/index.js';
import Common from '../common'
var Js = Common('areaMenu')
export default {
  name: 'areaMenu',
  components: {},
  mixins: [Js],
  data() {
    return {
      mainData: [],
      self: this,
      table_arguments: {
        height: '70%',
        row_key: 'id',
        tree_props: '',
        default_expand_all: false,
        expand: false,
        selection: false,
        cell_style: {},
        tree_props: {
          children: 'children',
        }
      },
      searchForm: {
        status: '1'
      },
      fields: [{
          key: 'id',
          label: 'ID',
          width: '100',
          application: ['table'],
          componentName: 'sls-input',
          listType: 'mini',
        },
        {
          key: "hasAuth",
          width: '100',
          label: '是否有权限',
          application: ['添加', '编辑'],
          componentName: 'sls-checkbox',
          listType: 'mini',
          customSlot: 'hasAuth',
          optionsName: 'hasAuthOptions',
          defaultProps: {
            label: 'text',
            value: 'value',
          },
        },
        {
          key: "isFirst",
          width: '100',
          label: '是否默认跳转',
          application: ['添加', '编辑'],
          componentName: 'sls-checkbox',
          listType: 'mini',
          customSlot: 'isFirst',
        },
        {
          key: "name",
          label: '名称',
          application: ['添加', '编辑'],
          componentName: 'sls-input',
          listType: 'mini',
        },
        {
          key: "router",
          label: '路由',
          application: ['添加', '编辑'],
          componentName: 'sls-input',
          listType: 'mini',
        },
        {
          key: "isShow",
          label: '是否显示',
          width: '100',
          application: ['添加', '编辑'],
          componentName: 'sls-checkbox',
          listType: 'mini',
          customSlot: 'isShow',
          optionsName: 'isShowOptions',
          defaultProps: {
            label: 'text',
            value: 'value',
          },
        },
        {
          key: "button",
          label: '按钮',
          application: ['添加', '编辑'],
          customSlot: 'button',
          componentName: 'sls-input',
          listType: 'mini',

        },
      ],

      // 需要给分页组件传的信息
      pagination: {
        current_page: 1,
        total: 0,
        page_sizes: [3, 9, 12, 24],
        layout: 'total, sizes, prev, pager, next, jumper',
        pagesize: 10,
        is_page: true,
      },

      // 搜索配置
      search_data: {
        fields: [{
          key: 'title',
          label: '标题'
        }],
        default_value: {
          title: ''
        }
      },

      // 按钮配置
      btn_info: [{
          type: 'danger',
          icon: 'delete',
          size: 'mini',
          funcType: 'submit',
          position: 'header',
          text: function(data) {
            return '更新权限'
          },
          func: {
            apiName: function(data) {
              return "api_updateAuth"
            },
            postData: function(self) {
              var postData = {
                behavior:self.behavior,
                user_no:self.user_no,
                role:self.role,
                auth:self.auth,
              };
              return postData;
            }
          },
        },
        {
          type: 'info',
          icon: 'edit',
          size: 'mini',
          position: 'header',
          text: function(self) {
            return '权限全选'
          },
          funcType: 'func',
          func: {
            func: function(self) {
              self.mainData = self.menuByAuth(self.orginMenu, self.orginAllAuth, 1);
              self.auth = self.$$cloneForm(self.orginAllAuth);
            },
          },
        },
        {
          type: 'info',
          icon: 'edit',
          size: 'mini',
          position: 'header',
          text: function(self) {
            return '使用角色权限'
          },
          funcType: 'func',
          func: {
            func: function(self) {
              console.log('self.roleAuth',self.roleAuth)
              self.mainData = self.menuByAuth(self.orginMenu, self.roleAuth, 1);
              self.auth = self.$$cloneForm(self.roleAuth);
            },
          },
        },
        {
          type: 'info',
          icon: 'edit',
          size: 'mini',
          position: 'header',
          text: function(data) {
            return '返回'
          },
          funcType: 'func',
          func: {
            func: function(self) {
              self.$router.push(self.path);
            },
          },
        },
      ],

      paginate: {
        count: 0,
        currentPage: 1,
        pagesize: 10,
        is_page: true,
        page_sizes: [10, 30, 60, 90],
        layout: 'total, sizes, prev, pager, next, jumper',
      },
      searchItem: {

      },
	  startSearchItem: {
	  
	  },
      optionData: {
        labelOptions: [],
        statusOptions: [{
          text: '启用',
          value: 1
        }, {
          text: '禁用',
          value: -1
        }],
        isShowOptions: ['显示', '隐藏']
      },
      defaultProps: {
        children: 'children',
        label: 'name',
        value: 'id',
      },
      otherData: {
        deleteApiName: 'api_label_update',
      },
      defaultChecked: [],
      formLabelWidth: 'auto',
      btnName: '',
      formData: {},
      btnNow: {},
      submitData: {},
      originFormData: {},
      selectionArray: [],
      dialog: {
        dialogFormVisible: false
      },
      checkedCities: ['显示'],
      auth: [],
      c_auth: [],
      c_menu: [],
      router_to: '',
      isCheck: true,
      isNoCheck: false,
      user_no: '',
      path: '',
      orginMenu: [],
      orginAllAuth: []



    }

  },
  mounted() {
    console.log('this.$route.params', this.$route.params)
    this.init()
  },
  computed: {
    token: function() {
      return this.$store.getters.getToken
    }
  },
  watch: {
    $route(to, from) {
      console.log(to)
      this.init()
    },
    token() {

    }
  },
  methods: {


    /**
     * 初始化
     */
    init() {

      const self = this;
      console.log(self.$route.params)
      if (self.$route.params.role) {
        self.searchItem.role = self.$route.params.role;
        //self.searchItem.user_no = self.$route.params.user_no;
         
        self.searchItem.behavior = 1;
        self.initAuthData();
      };
      self.role = self.$route.params.role;
      self.user_no = self.$route.params.user_no;
      self.path = self.$route.params.path;
      if (!self.$route.params.type) {
        self.behavior = 0;
        self.auth = self.$route.params.defaultChecked;
        //self.user_no = self.$route.params.user_no;
        self.initMainData();
      }else{
         self.behavior = 1;
      };

    },

    clearSearch() {
      const self = this;
      self.searchItem = {};
      self.initMainData(true);
    },


    async initAuthData() {

      const self = this;
      console.log('initAuthData')
      const postData = {};

      postData.token = self.$store.getters.getToken;
      postData.searchItem = self.$$cloneForm(self.searchItem);
      var res = await self.$$api_authGet({
        data: postData
      });
      console.log('res', res)
      self.roleAuth = res.info;
      if (self.$route.params.type == 'role') {
        self.auth = self.roleAuth;
        self.initMainData();
      };

    },

    menuByAuth(oMenu, auth, type) {
      const self = this;
      /* type=0,没有权限会删除菜单；type=1,权限只会改变是否选中*/
      var resMenu = [];
      var auth = auth;
      var type = type;
      var oMenu = oMenu;

      pushItemsExclude(oMenu, resMenu);

      console.log('auth', auth);
      console.log('oMenu', oMenu);
      console.log('resMenu', resMenu);
      return resMenu;

      function pushItemsExclude(oMenu, resMenu) {

        for (var i = 0; i < oMenu.length; i++) {
          if (auth != 'All' && Array.isArray(auth) && auth.indexOf(oMenu[i].id) == -1 && type != 1) {
            continue;
          };
          if (type == 0) {
            self.orginAllAuth.push(oMenu[i].id);
          };
          var childItem = {};
          childItem = self.$$cloneForm(oMenu[i]);

          if (auth.indexOf(oMenu[i].id) > -1 || auth == 'All') {
            childItem.hasAuth = true;
          } else {
            childItem.hasAuth = false;
          };

          if (auth.indexOf(oMenu[i].id + '_isShow') > -1 || auth == 'All') {
            childItem.isShow = true;
          } else {
            childItem.isShow = false;
          };
          if (auth.indexOf('first_' + oMenu[i].router) > -1) {
            self.router_to = oMenu[i].router;
          };

          if (JSON.stringify(childItem) != '{}' && oMenu[i].button && oMenu[i].button.length > 0) {
            childItem.hasButton = [];
            for (var j = 0; j < oMenu[i].button.length; j++) {
              if (auth.indexOf(oMenu[i].id + '_' + oMenu[i].button[j]) > -1 || auth == 'All') {
                childItem.hasButton.push(oMenu[i].button[j]);
              };
              if (type == 0) {
                self.orginAllAuth.push(oMenu[i].id + '_' + oMenu[i].button[j]);
              };
            };
          };

          if (JSON.stringify(childItem) != '{}' && oMenu[i].children && oMenu[i].children.length > 0) {
            var newArray = self.$$cloneForm(oMenu[i].children);
            childItem.children = [];
            pushItemsExclude(newArray, childItem.children);
          };

          if (JSON.stringify(childItem) != '{}') {
            resMenu.push(childItem);
          };

        };

      };

    },


    async initMainData() {

      const self = this;
      var results = [];
      var c_results = [];
      // var data = self.$$cloneForm(self.menu);
      var data = self.$$cloneForm(gbs.menu);


      if (self.$store.getters.getUserinfo.primary_scope == 90) {
        var testData = 'All';
      } else {
        var testData = self.$store.getters.getUserinfo.auth;
      };
      console.log('testData',testData)
      var firstMenu = self.menuByAuth(data, testData, 0);
      console.log('firstMenu',firstMenu)
      if (self.$route.params.primary_scope && self.$route.params.primary_scope == 90) {
        var checkData = 'All';
      } else {
        var checkData = self.auth;
      };
      self.orginMenu = self.menuByAuth(firstMenu, checkData, 1);
      self.mainData = self.$$cloneForm(self.orginMenu);
      console.log('self.mainData', self.mainData)
      return;
      console.log('testData', testData);
      console.log('checkData', checkData);
      pushItemsExclude(data, results, c_results, testData);
      self.mainData = results;
      self.c_menu = c_results;
      console.log('initMainData-results', results);
      console.log('initMainData-self.c_auth', self.c_auth);
      return;

      function pushItemsExclude(data, results, c_results, testData) {

        for (var i = 0; i < data.length; i++) {
          if (testData != 'All' && Array.isArray(testData) && testData.indexOf(data[i].id) == -1) {
            break;
          };
          var childItem = {};
          var c_childItem = {};
          if (checkData != 'All') {
            childItem = data[i];
            if (checkData.indexOf('first_' + data[i].router) > -1) {
              self.router_to = data[i].router;
            };
            c_childItem = self.$$cloneForm(data[i]);
            if (checkData.indexOf(data[i].id) > -1) {
              childItem.hasAuth = true;
            } else {
              childItem.hasAuth = false;
            };
            c_childItem.hasAuth = true;
            self.c_auth.push(data[i].id);
          } else if (checkData == 'All') {
            childItem = data[i];
            childItem.hasAuth = true;
            c_childItem = self.$$cloneForm(data[i]);
            c_childItem.hasAuth = true;
            self.c_auth.push(data[i].id);
          };

          if (JSON.stringify(childItem) != '{}' && data[i].button && data[i].button.length > 0) {
            childItem.hasButton = [];
            c_childItem.hasButton = [];
            for (var j = 0; j < data[i].button.length; j++) {
              if (checkData.indexOf(data[i].id + '_' + data[i].button[j]) > -1) {
                childItem.hasButton.push(data[i].button[j]);
              };
              c_childItem.hasButton.push(data[i].button[j]);
              self.c_auth.push(data[i].id + '_' + data[i].button[j]);
            };
          };

          if (JSON.stringify(childItem) != '{}' && data[i].children && data[i].children.length > 0) {
            var newArray = self.$$cloneForm(data[i].children);
            childItem.children = [];
            c_childItem.children = [];
            pushItemsExclude(newArray, childItem.children, c_childItem.children);
          };

          if (JSON.stringify(childItem) != '{}') {
            results.push(childItem);
            c_results.push(c_childItem);
          };

        };

      };

    },


    isFirstChange(router) {
      const self = this;
      self.isCheck = true;
      self.isNoCheck = false;
      if (!router || router == 'undefined') {
        return;
      };

      var before_key = 'first_' + self.router_to;
      var key = 'first_' + router;
      if (key == before_key) {
        self.auth.splice(self.auth.indexOf(before_key), 1);
        self.router_to = '';
      } else if (self.auth.indexOf(before_key) > -1) {
        self.auth.splice(self.auth.indexOf(before_key), 1);
        self.auth.push(key);
        self.router_to = router;
      } else {
        self.auth.push(key);
        self.router_to = router;
      };

    },


    isFirstTest(router) {
      const self = this;
      var key = 'first_' + router;

      if (self.auth.indexOf(key) > -1) {
        return true;
      } else {
        return false;
      };
    },


    buttonChange(item) {
      console.log('buttonChange', item);
      const self = this;
      for (var i = 0; i < item.data.button.length; i++) {
        var key = item.data.id + '_' + item.data.button[i];
        if (item.data.hasButton.indexOf(item.data.button[i]) > -1) {
          self.authChange(key, true);
        } else {
          self.authChange(key, false);
        };
      }

    },


    hasAuthChange(item) {
      console.log('hasAuthChange', item);
      const self = this;
      self.authChange(item.data.id, item.data.hasAuth);
      var key = item.data.id;
      self.authChange(key, item.data.hasAuth);
    },


    isShowChange(item) {
      console.log('isShowChange', item);
      const self = this;
      var key = item.data.id + '_isShow';
      self.authChange(key, item.data.isShow);
      self.authChange(item.id, item.data.isShow);
    },


    authChange(key, value) {
      const self = this;
      console.log('key', key)
      var index = self.auth.indexOf(key);

      console.log('index', index)
      if (value && index == -1) {
        self.auth.push(key)
      } else if (!value && index > -1) {
        self.auth.splice(index, 1)
      };
      console.log('self.auth', self.auth)
    },




  }


}
